/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Jul 7, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.toolkits;

import javax.swing.*;
import java.awt.*;
import java.util.Date;

public class Debug {

    /**
     * Debug level.
     * <li>0 - only errors will be displayed</li>
     * <li>1 - errors and warnings will be displayed</li>
     * <li>2 - all messages will be displayed</li>
     *
     */
    public static final int DEBUG = 2;

    static String currDateTime() {

        return new Date().toString();
    }

    /**
     * Prints error message to the System.err
     *
     * @param message The error message.
     */
    public static void error(String message) {
        System.err.println("ERROR (" + currDateTime() + "): " + message);
    }

    /**
     * Prints error message to the System.err then print exception stack trace.
     *
     * @param message The error message.
     * @param ex The exeception thrown.
     */
    public static void error(String message, Exception ex) {
        System.err.println("ERROR (" + currDateTime() + "): " + message);
        ex.printStackTrace();
    }

    /**
     * Prints warning message to the System.out.
     * Message only printed when debug level >= 1
     *
     * @param message The warning message.
     */
    public static void warning(String message) {
        if (DEBUG != 0)
            System.out.println("WARNING (" + currDateTime() + "): " + message);
    }

    /**
     * Prints debug message to the System.out.
     * Message only printed when debug level == 2
     *
     * @param message The debug message.
     */
    public static void debug(String message) {
        if (DEBUG == 2)
            System.out.println("DEBUG (" + currDateTime() + "): " + message);
    }

    /**
     * Displays error message to the user
     *
     * @param parent determines the <code>Frame</code> in which the dialog is displayed;
     *  if <code>null</code>, or if the <code>parent</code> has no <code>Frame</code>,
     *  a default <code>Frame</code> is used
     * @param message The error message.
     */
    public static void displayError(Component parent, String message) {
        JOptionPane.showMessageDialog(parent, message, "Error", JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Displays warning message to the user
     *
     * @param parent determines the <code>Frame</code> in which the dialog is displayed;
     *  if <code>null</code>, or if the <code>parent</code> has no <code>Frame</code>,
     *  a default <code>Frame</code> is used
     * @param message The error message.
     */
    public static void displayWarning(Component parent, String message) {
        JOptionPane.showMessageDialog(parent, message, "Warning", JOptionPane.WARNING_MESSAGE);
    }

    /**
     * Displays question message with two option ('Yes' and 'No') to the user
     *
     * @param parent determines the <code>Frame</code> in which the dialog is displayed;
     *  if <code>null</code>, or if the <code>parent</code> has no <code>Frame</code>,
     *  a default <code>Frame</code> is used
     * @param title The question title.
     * @param message The question message.
     *
     * @return <code>true</code> if user selected 'Yes' option; otherwise <code>false</code>
     */
    public static boolean displayConfirmation(Component parent, String title, String message) {
        return JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(parent, message, title,
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    }
}
